/**
 *
 */
package com.naiterui.ehp.bs.emr.vo.patient;

import java.util.List;
import java.util.Optional;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;

import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import org.slf4j.LoggerFactory;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 基本病情
 *

 * @since 2.16
 */
@SuppressWarnings("WeakerAccess")
@ApiModel(value = "DocumentVO", description = "患者基本病情VO")
public class DocumentVO implements java.io.Serializable {
    private static final Logger LOGGER = LoggerFactory.getLogger(DocumentVO.class);
    private static final long serialVersionUID = -3252153843481656319L;

    @ApiModelProperty(value = "病例ID")
    private String caseId;

    @ApiModelProperty(value = "关联医生ID")
    private Long doctorId;
    /**
     * 哺乳或怀孕状态
     */
    @ApiModelProperty(value = "哺乳或怀孕状态:1-哺乳中；2-怀孕中；3-不知道；4-都不是")
    private Integer pregnancy;
    /**
     * <身高>
     */
    @ApiModelProperty(value = "身高")
    @Max(value = 300, message = "身高不能超过300")
    @Min(value = 1, message = "身高不能低于1")
    private Integer height;
    /**
     * <体重>
     */
    @ApiModelProperty(value = "体重")
    @Max(value = 1000, message = "体重不能超过300")
    @Min(value = 1, message = "体重不能低于1")
    private Integer weight;
    /**
     * 病情描述
     */
    @ApiModelProperty(value = "病情描述")
    private String description;

    @ApiModelProperty(value = "请求惠美唯一标识")
    private String amcSeqNum;
    /**
     * amc诊断id
     */
    @ApiModelProperty(value = "amc诊断id")
    private String amcId;
    /**
     * <过敏药物>
     */
    @ApiModelProperty(value = "药物过敏史")
    private MSelectOrOthersVO medicAllergys;
    /**
     * <过往病史>
     */
    @ApiModelProperty(value = "既往病史")
    private MSelectOrOthersVO pastDiseases;
    /**
     * <家族病史。数组size=0标识无家族病>
     */
    @ApiModelProperty(value = "家族病史")
    private List<MFamilyDiseaseVO> familyDiseases;
    /**
     * <遗传病>
     */
    @ApiModelProperty(value = "遗传病史")
    private MSelectOrOthersVO hereditaryDiseases;
    /**
     * <吸烟情况>
     */
    @ApiModelProperty(value = "吸烟情况")
    private MSmokeVO smoke;

    /**
     * <饮酒情况>
     */
    @ApiModelProperty(value = "饮酒情况")
    private MDrinkVO drink;

    /**
     * 参数校验
     *
     * @throws BusinessException
     */
    public void validation() throws BusinessException {
        // 药物过敏史
        MSelectOrOthersVO medicAllergys = this.getMedicAllergys();
        if (medicAllergys != null) {
            medicAllergys.validation();
        }

        // 既往病史
        MSelectOrOthersVO pastDiseases = this.getPastDiseases();
        if (pastDiseases != null) {
            pastDiseases.validation();
        }

        // 家族病史
        List<MFamilyDiseaseVO> familyDiseases = this.getFamilyDiseases();
        for (MFamilyDiseaseVO familyDisease : Optional.ofNullable(familyDiseases).orElse(Lists.newArrayList())) {
            if (familyDisease == null) {
                continue;
            }
            MSelectOrOthersVO disease = familyDisease.getDisease();
            if (disease == null) {
                continue;
            }
            disease.validation();
        }

        // 遗传病
        MSelectOrOthersVO hereditaryDiseases = this.getHereditaryDiseases();
        if (hereditaryDiseases != null) {
            hereditaryDiseases.validation();
        }

        MSmokeVO smoke = this.getSmoke();
        // 吸烟，校验吸烟数量
        if (smoke != null && String.valueOf(MSmokeVO.SMOKE_STATUS_YES).equals(smoke.getStatus())) {
            // 日吸烟数量（支）
            String perdayNum = smoke.getPerdayNum();
            // 吸烟开始年龄（岁）
            String smokeStart = smoke.getSmokeStart();

            // 日吸烟量1-100
            int perdayNumInt = Integer.parseInt(perdayNum);
            if (perdayNumInt < 1 || perdayNumInt > 100) {
                LOGGER.info("日吸烟量值太大！ perdayNumInt: {}", perdayNumInt);
                throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_SMOKE_PERDAY_INVALID);
            }

            // 开始吸烟年龄1~99
            int smokeInt = Integer.parseInt(smokeStart);
            if (smokeInt < 1 || smokeInt > 99) {
                LOGGER.info("吸烟开始年龄值太大！ smokeInt: {}", smokeInt);
                throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_SMOKE_AGE_INVALID);
            }
        }

        MDrinkVO drink = this.getDrink();
        if (drink != null) {
            Integer frequency = drink.getFrequency();
            // 偶尔、每天饮酒
            if (frequency != null && (MDrinkVO.DRINK_STATUS_SOMETIME == frequency || MDrinkVO.DRINK_STATUS_EVERYDAY == frequency)) {
                String perdayNum = drink.getPerdayNum();
                if (StringUtils.isBlank(perdayNum)) {
                    LOGGER.info("日饮酒量为空,perdayNum: {}", perdayNum);
                    throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_DRINK_PERDAY_INVALID);
                }
                if (perdayNum.length() < 1 || perdayNum.length() > 2) {
                    LOGGER.info("日饮酒量填写不正确,perdayNum: {}", perdayNum);
                    throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_DRINK_PERDAY_INVALID);
                }

                String drinkStart = drink.getDrinkStart();
                if (StringUtils.isBlank(drinkStart)) {
                    LOGGER.info("开始饮酒年龄为空,drinkStart: {}", drinkStart);
                    throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_DRINK_START_AGE_INVALID);
                }
                if (drinkStart.length() < 1 || drinkStart.length() > 3) {
                    LOGGER.info("开始饮酒年龄不正确,drinkStart: {}", drinkStart);
                    throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_DRINK_START_AGE_INVALID);
                }

                // 已戒酒
                if (String.valueOf(MDrinkVO.DRINK_STATUS_NO).equals(drink.getDrinkStatus())) {
                    String stopAge = drink.getStopAge();
                    if (StringUtils.isBlank(stopAge)) {
                        LOGGER.info("戒酒年龄为空,stopAge: {}", stopAge);
                        throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_DRINK_STOP_AGE_INVALID);
                    }
                    if (stopAge.length() < 1 || stopAge.length() > 3) {
                        LOGGER.info("戒酒年龄不正确,stopAge: {}", stopAge);
                        throw new BusinessException(ExceptionCodes.PATIENT_DISEASE_DRINK_STOP_AGE_INVALID);
                    }
                }

                MSelectOrOthersVO wines = drink.getWines();
                // 其他酒类
                if (wines != null) {
                    wines.validation();
                }
            }
        }
    }

    public String getCaseId() {
        return this.caseId;
    }

    public void setCaseId(String caseId) {
        this.caseId = caseId;
    }

    public Long getDoctorId() {
        return this.doctorId;
    }

    public void setDoctorId(Long doctorId) {
        this.doctorId = doctorId;
    }

    public Integer getPregnancy() {
        return this.pregnancy;
    }

    public void setPregnancy(Integer pregnancy) {
        this.pregnancy = pregnancy;
    }

    public Integer getHeight() {
        return this.height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer getWeight() {
        return this.weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAmcId() {
        return this.amcId;
    }

    public void setAmcId(String amcId) {
        this.amcId = amcId;
    }

    public MSelectOrOthersVO getMedicAllergys() {
        return this.medicAllergys;
    }

    public void setMedicAllergys(MSelectOrOthersVO medicAllergys) {
        this.medicAllergys = medicAllergys;
    }

    public MSelectOrOthersVO getPastDiseases() {
        return this.pastDiseases;
    }

    public void setPastDiseases(MSelectOrOthersVO pastDiseases) {
        this.pastDiseases = pastDiseases;
    }

    public List<MFamilyDiseaseVO> getFamilyDiseases() {
        return this.familyDiseases;
    }

    public void setFamilyDiseases(List<MFamilyDiseaseVO> familyDiseases) {
        this.familyDiseases = familyDiseases;
    }

    public MSelectOrOthersVO getHereditaryDiseases() {
        return this.hereditaryDiseases;
    }

    public void setHereditaryDiseases(MSelectOrOthersVO hereditaryDiseases) {
        this.hereditaryDiseases = hereditaryDiseases;
    }

    public MSmokeVO getSmoke() {
        return this.smoke;
    }

    public void setSmoke(MSmokeVO smoke) {
        this.smoke = smoke;
    }

    public MDrinkVO getDrink() {
        return this.drink;
    }

    public void setDrink(MDrinkVO drink) {
        this.drink = drink;
    }

    public String getAmcSeqNum() {
        return this.amcSeqNum;
    }

    public void setAmcSeqNum(String amcSeqNum) {
        this.amcSeqNum = amcSeqNum;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
